	var page_num = 0;
	var page_limit = 5;
	
	var data = [];
	
	var localData = JSON.parse(sessionStorage.getItem("list"));
	

		// for (let i = 0; i < localData.length; i++) {
		// 	data.push(localData[i]);
		// }

	window.onload = function(){

		selfilelist();
		//处理信息
		let userinfo = sessionStorage.getItem("username");
			userinfo =  JSON.parse(userinfo);
			
		//总空间
		let cloud_start_size = document.getElementById("cloud_start_size");
			text11 = document.createTextNode(count(userinfo.level_cloud_size));
			cloud_start_size.appendChild(text11);
			
		//已使用空间
		let cloud_out_size = document.getElementById("cloud_out_size");
		text12 = document.createTextNode(count(userinfo.cloud_size));
		cloud_out_size.appendChild(text12);
			
		//未使用空间
		let cloud_no_size = document.getElementById("cloud_no_size");
		let min = userinfo.level_cloud_size - userinfo.cloud_size;
		text13 = document.createTextNode(count(min));
		cloud_no_size.appendChild(text13);
			
			
			
		
	}
	
	function show(){
	
	console.log("列表show")
		
		data = [];
		localData = JSON.parse(sessionStorage.getItem("list"));
				
		console.log(localData)
		for (let i = 0; i < localData.length; i++) {
			data.push(localData[i]);
		}
		console.log(data.length)

		//获取ID
			var file_list = document.getElementById("list");
			file_list.innerHTML = '';
			for (let i = 0; i < data.length; i++) {
				let tr = document.createElement("tr");
				if(i > 4){
					tr.setAttribute("style","display:none;")
				}
				let td = document.createElement("td");
				let input = document.createElement("input");
				input.setAttribute("type", "checkbox");
				input.setAttribute("style", "opacity: 1;margin-left: 5px;position: unset;")
		
		
				let td2 = document.createElement("td");
				let text2 = document.createTextNode(data[i].name);
				let input2 = document.createElement("input");
				input2.setAttribute("type", "hidden");
				input2.setAttribute("value", data[i].id + "")
				td2.appendChild(text2);
		
				let td3 = document.createElement("td");
				let span = document.createElement("span");
				span.setAttribute("class", "text-muted");
		
				
		let img = document.createElement("i");
				img.setAttribute("class", "fa fa-clock-o");
				let spanText = document.createTextNode(new Date(data[i].create_time));
				span.appendChild(img);
				span.appendChild(spanText);
		
				
		td3.appendChild(span);
		
				let td4 = document.createElement("td");
				let td4span = document.createElement("span");
		
				td4span.setAttribute("class", "badge badge-pill badge-danger");
				let text4 = document.createTextNode(data[i].size);
				td4span.appendChild(text4)
				td4.appendChild(td4span);
		
				
		let td5 = document.createElement("td");
				let btn1 = document.createElement("button");
				btn1Text = document.createTextNode("下载");
				btn1.setAttribute("onclick","download(this)")
				btn1.appendChild(btn1Text);
				btn1.setAttribute("class", "waves-effect waves-light btn btn-info btn-xs");
				let btn2 = document.createElement("button");
				btn2Text = document.createTextNode("重命名");
				btn2.setAttribute("onclick","updateFileName(this)")
				btn2.appendChild(btn2Text);
				btn2.setAttribute("class", "waves-effect waves-light btn btn-info btn-xs");
				let btn3 = document.createElement("button");
				btn3Text = document.createTextNode("删除");
				btn3.setAttribute("onclick", "Del(this)");
		
				btn3.appendChild(btn3Text);
				btn3.setAttribute("class", "waves-effect waves-light btn btn-info btn-xs ");
		
				td5.appendChild(btn1);
				td5.appendChild(btn2);
				td5.appendChild(btn3);
		
				td.appendChild(input2);
				td.appendChild(input);
				tr.appendChild(td);
				tr.appendChild(td2);
				tr.appendChild(td3);
				tr.appendChild(td4);
				tr.appendChild(td5);
				file_list.appendChild(tr);
		
			}
			
		//分页
		pagelist()
		page_show(page_num)
		
	
			
	}

	
	function searchShow(){

		localData = JSON.parse(sessionStorage.getItem("list"));
		data = [];
		for (let i = 0; i < localData.length; i++) {
			data.push(localData[i]);
		}


		//获取ID
		var file_list = document.getElementById("list");
		file_list.innerHTML = '';

		for (let i = 0; i < data.length; i++) {
			let tr = document.createElement("tr");
			if(i > 4){
				tr.setAttribute("style","display:none;")
			}
			let td = document.createElement("td");
			let input = document.createElement("input");
			input.setAttribute("type", "checkbox");
			input.setAttribute("style", "opacity: 1;margin-left: 5px;position: unset;")


			let td2 = document.createElement("td");
			let text2 = document.createTextNode(data[i].name);
			let input2 = document.createElement("input");
			input2.setAttribute("type", "hidden");
			input2.setAttribute("value", data[i].id + "")
			td2.appendChild(text2);

			let td3 = document.createElement("td");
			let span = document.createElement("span");
			span.setAttribute("class", "text-muted");


			let img = document.createElement("i");
			img.setAttribute("class", "fa fa-clock-o");
			let spanText = document.createTextNode(new Date(data[i].create_time));
			span.appendChild(img);
			span.appendChild(spanText);


			td3.appendChild(span);

			let td4 = document.createElement("td");
			let td4span = document.createElement("span");

			td4span.setAttribute("class", "badge badge-pill badge-danger");
			let text4 = document.createTextNode(data[i].size);
			td4span.appendChild(text4)
			td4.appendChild(td4span);


			let td5 = document.createElement("td");
			let btn1 = document.createElement("button");
			btn1Text = document.createTextNode("下载");
			btn1.setAttribute("onclick","download(this)")
			btn1.appendChild(btn1Text);
			btn1.setAttribute("class", "waves-effect waves-light btn btn-info btn-xs");
			let btn2 = document.createElement("button");
			btn2Text = document.createTextNode("重命名");
			btn2.setAttribute("onclick","updateFileName(this)")
			btn2.appendChild(btn2Text);
			btn2.setAttribute("class", "waves-effect waves-light btn btn-info btn-xs");
			let btn3 = document.createElement("button");
			btn3Text = document.createTextNode("删除");
			btn3.setAttribute("onclick", "Del(this)");

			btn3.appendChild(btn3Text);
			btn3.setAttribute("class", "waves-effect waves-light btn btn-info btn-xs ");

			td5.appendChild(btn1);
			td5.appendChild(btn2);
			td5.appendChild(btn3);

			td.appendChild(input2);
			td.appendChild(input);
			tr.appendChild(td);
			tr.appendChild(td2);
			tr.appendChild(td3);
			tr.appendChild(td4);
			tr.appendChild(td5);
			file_list.appendChild(tr);

		}


		pagelist()
		page_show(page_num)
	}
function add() {

	let file_name = prompt("请输入您上传的文件名字");
	let file_size = prompt("请输入上传文件的大小，单位B");
	let file_type = prompt("请输入文件类型 1 txt 2 html 3 MP3");
	
	if(file_name.length == 0 || file_size.length == 0 || file_type.length == 0){
		alert("内容填写不完整")
		return;
	}
	$.ajax("/addFile",{
		type:"POST",
		data:{
			file_name:file_name,
			file_size:file_size,
			file_type:file_type
		},
		success:function (data,textStatus){
	
			let errMsg = JSON.stringify(JSON.parse(data).errMsg);
	
			alert(errMsg)
	
		}
	})



	}
	
function openFile(){
	
	let file  = $("#file").click();
	
}

function upload(e){
	
	
	if(e == null){
		alert("您没有选择文件")
	}
	let filename = e.files[0].name;
	let filesize = e.files[0].size;
	let filesrc = e.value;
	
	$.ajax("/upload",{
		type:"POST",
		data:{
			file_name:filename,
			file_size:filesize,
			filesrc:filesrc
		},
		success:function (data,textStatus){
	
			let errMsg = JSON.stringify(JSON.parse(data).errMsg);
	
			alert(errMsg)
			selfilelist()
			show()
	
		}
	})
	
	
}
function Del(a) {

	let id = a.parentNode.parentNode.firstElementChild.firstElementChild.value;

	del(id)
	 show()
}


function DelAll() {

	let list = document.getElementById("list").children;
	var count = 0;
	var length = data.length;
	for (let i = 0; i < list.length; i++) {
		let checkedTags = list[i].firstElementChild.lastElementChild
		//如果第一个字段的状态时true
		if (checkedTags.checked == true) {
			//查找对应的id
			let id = checkedTags.parentElement.firstElementChild.value;
			// 调用删除方法
			del(id)
		} else {
			count++;
		}

	}
	// console.log(count);
	// console.log(data.length);

	if (count == length) {
		alert("您没有选择要删除的行")
	} else {
		let s = JSON.stringify(data);
		localStorage.setItem("list", s);

		alert("删除成功");
		show()
	}

}

//下载方法
function download(a){
	//获取文件ID
	let id = a.parentNode.parentNode.firstElementChild.firstElementChild.value;
	alert(id)
	$.ajax("/download",{
		type:"POST",
		data:{
			id:id
		},
		success:function (data,textStatus){

			let errMsg = JSON.stringify(JSON.parse(data).errMsg);

			alert(errMsg)

		}
	})
}




function formatDateTime(date, format) {
  const o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
    'H+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds(), // 毫秒
    a: date.getHours() < 12 ? '上午' : '下午', // 上午/下午
    A: date.getHours() < 12 ? 'AM' : 'PM', // AM/PM
  };
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
      );
    }
  }
  return format;
}


//分页
function pagelist(){
	
	let page_top_num = page_num-1;
	if(page_top_num < 0){
		page_top_num = 0;
	}
	
	let page_down_num = page_num+1;

	//获取长度
	let length = JSON.parse(sessionStorage.getItem("list")).length;
	let page = document.getElementById("page_list");
	// console.log(length)
	// <li class="paginate_button page-item ">
	// <a href="#" aria-controls="complex_header" data-dt-idx="2" tabindex="0" class="page-link">2</a>
	// </li>
	//插入上一页
	let str  =  `<li class="paginate_button page-item previous" id="complex_header_previous">
								<a href="#" aria-controls="complex_header" data-dt-idx="0" tabindex="0"
									class="page-link" onclick="page_show(${page_top_num},0)">Previous</a>
							</li>`;
	for (var i = 0; i < length/5; i++) {
	
		
		
		if(page_num == i ){
			
			str += `<li class="paginate_button page-item active">`;
		}else{
			str += `<li class="paginate_button page-item">`;
		}
		
		str += `
					<a href="#" onclick="page_show(${i})" aria-controls="complex_header" data-dt-idx="6" tabindex="0" class="page-lin">${i+1}</a>
				</li>`;
	}
	//插入下一页
	str  += `<li class="paginate_button page-item next" id="complex_header_next">
						<a href="#" aria-controls="complex_header" data-dt-idx="7" tabindex="0"
								class="page-link" onclick="page_show(${page_down_num},1)">Next</a>
						</li>`;
	page.innerHTML = str;
}


function page_show(a,type){


//判断是否超过最大页数
//获取长度
let length = JSON.parse(sessionStorage.getItem("list")).length;
if(a >= length/page_limit){
	alert("已经是最后一页了")
	return;
}
// 获取id 
let list = document.getElementById("list");
//获取子类
let listNew = list.children;

//起始位置等于 页数 乘以每页数量-1

let star_num = a * page_limit;
console.log(star_num);
let end_num = 0;
if(star_num < 0){
	star_num = 0;
	end_num = page_limit;
}else{
	end_num = star_num + page_limit +1;
}

// 显示提示信息 获取ID

let complex_header_info = document.getElementById("complex_header_info");
complex_header_info.innerHTML = '';
let tip = "Showing "+(star_num+1)+" to "+end_num +" of "+length+" entries"
let text = document.createTextNode(tip);
complex_header_info.appendChild(text);


for (let i = 0; i < listNew.length; i++) {
	if(i >=star_num && i <(star_num+page_limit) ){
		//设置属性为可见
		listNew[i].setAttribute("style","");
	}else{
		listNew[i].setAttribute("style","display:none");
	}
	
}
page_num = a;
// console.log("翻页后"+a);
pagelist();
}

